﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using IKE.LCAFormat.ILCD.Common;

namespace IKE.LCAFormat.ILCD
{
    public static class Helper
    {
        public static string LangKey { get; set; }

        public static string GetLocal(this List<StringMultiLang> lst)
        {
            return lst.GetValue(LangKey);
        }

        public static string GetValue(this List<StringMultiLang> lst, string lang)
        {
            if (lst.Count == 0) return "";
            StringMultiLang obj = lst.Find(o => o.lang == lang);
            if (obj == null && lang.Length>2)
            {
                string lang2key = LangKey.Substring(0, 2);
                obj = lst.Find(o => o.lang == lang2key);
            }
            if (obj != null)
                return obj.Value;
            else
                return lst[0].Value;
        }

        public static void SetLocal(this List<StringMultiLang> lst, string value)
        {
            lst.SetValue(LangKey, value);
        }

        public static void SetValue(this List<StringMultiLang> lst, string lang, string value)
        {
            StringMultiLang obj = lst.Find(o => o.lang == lang);
            if (obj == null)
            {
                lst.Add(new StringMultiLang() { lang = lang, Value = value });
            }
            else
                obj.Value = value;
        }


        public static void AddOrUpdate(this List<StringMultiLang> lst, StringMultiLang item)
        {
            if (item.lang == "zh" || item.lang == "zh-CN") item.lang = "zh-Hans";
            var find = lst.Find(o => o.lang == item.lang);
            if (find != null)
                find.Value = item.Value;
            else
                lst.Add(item);
        }
    }
}
